package cn.chencq.utils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.chencq.moudle.lottery.entity.Lottery;

public class LotteryApiUtils {

	public static List<Lottery> getLotteryList(String lotteryType, int page, String date) {
		List<Lottery> lotteryList = new ArrayList<Lottery>();
		try {
			Connection conn = Jsoup.connect("https://wwwpc28.tw/?m=api&c=trend&a=getMoreLottery");
			conn.timeout(10000); // 10秒超时

			conn.data("lottery_type", lotteryType);
			if (!StringUtils.isEmpty(date)) {
				conn.data("time", date);
				conn.data("day", date);
				conn.data("page", String.valueOf(page));
			}

			Response response = conn.method(Method.POST).execute();
			String body = response.body();

			JSONObject json = JSONObject.parseObject(body);
			int status = json.getInteger("status");
			if (status == 0) {
				JSONObject data = json.getJSONObject("data");
				JSONArray list = data.getJSONArray("list");
				int size = list.size();
				for (int i = 0; i < size; i++) {
					JSONObject o = list.getJSONObject(i);

					Lottery lottery = new Lottery();
					lottery.setLotteryId(o.getString("lottery_id"));
					lottery.setLotteryType(Integer.parseInt(lotteryType));

					long timestamp = Long.valueOf(o.getString("lottery_date")) * 1000;
					lottery.setLotteryDate(new Timestamp(timestamp));

					String lotteryNumbers = o.getString("lottery_numbers");
					if (dealLotteryNumbers(lotteryType)) {
						lotteryNumbers = lotteryNumbers.substring(0, lotteryNumbers.lastIndexOf(","));
					}
					lottery.setLotteryNumbers(lotteryNumbers);

					if (dealContent(lotteryType)) {
						JSONArray array = o.getJSONArray("content");
						lottery.setLotteryContent(array.toJSONString());
					} else {
						lottery.setLotteryContent(o.getString("content"));
					}
					lotteryList.add(lottery);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lotteryList;
	}

	/**
	 *
	 * @Title: dealLotteryNumbers
	 * @Description: 是否需要对LotteryNumbers进行特殊处理
	 * @param lotteryType
	 * @return boolean
	 */
	private static boolean dealLotteryNumbers(String lotteryType) {
		if ("1".equals(lotteryType) || "3".equals(lotteryType)) {
			return true;
		}
		return false;
	}

	/**
	 *
	 * @Title: dealContent
	 * @Description: 是否需要对content进行特殊处理
	 * @param lotteryType
	 * @return boolean
	 */
	private static boolean dealContent(String lotteryType) {
		if ("1".equals(lotteryType) || "2".equals(lotteryType) || "3".equals(lotteryType) || "4".equals(lotteryType)
				|| "5".equals(lotteryType) || "6".equals(lotteryType) || "9".equals(lotteryType)
				|| "11".equals(lotteryType) || "13".equals(lotteryType)) {
			return true;
		}
		return false;
	}

	public static int getTotalPage(String lotterType) {
		int page = 6;
		switch (lotterType) {
		case "3": // 加拿大28
			page = 14;
			break;
		case "5": // 重庆时时彩
			page = 4;
			break;
		case "6": // 三分彩
			page = 16;
			break;
		case "7": // 香港六合彩
			page = 1;
			break;
		case "8": // 急速六合彩
			page = 10;
			break;
		case "9": // 急速赛车
			page = 16;
			break;
		case "10": // 百人牛牛
			page = 10;
			break;
		case "11": // 分分彩
			page = 48;
			break;
		case "13": // 欢乐骰宝
			page = 10;
			break;
		case "14": // 分分PK10
			page = 48;
			break;
		default:
			break;
		}
		return page;
	}
	
	public static List<Lottery> getSingaporeLotteryList(String lotteryType, int page, String date) {
		List<Lottery> lotteryList = new ArrayList<Lottery>();
		try {
			Connection conn = Jsoup.connect("http://th0168.com/ajax/AppGateWay.ashx");
			conn.timeout(10000); // 10秒超时

			conn.data("opt", "25");
			conn.data("info", "");//{"searchType":"2","lotteryID":"107","startTime":"2019-01-14","endTime":"2019-03-15","pageIndex":3,"pageSize":10}
			conn.data("auth","");// {"loginType":1, "UID":20000}

			Response response = conn.method(Method.POST).execute();
			String body = response.body();

			JSONObject json = JSONObject.parseObject(body);
			int status = json.getInteger("status");
			if (status == 0) {
				JSONObject data = json.getJSONObject("data");
				JSONArray list = data.getJSONArray("list");
				int size = list.size();
				for (int i = 0; i < size; i++) {
					JSONObject o = list.getJSONObject(i);

					Lottery lottery = new Lottery();
					lottery.setLotteryId(o.getString("lottery_id"));
					lottery.setLotteryType(Integer.parseInt(lotteryType));

					long timestamp = Long.valueOf(o.getString("lottery_date")) * 1000;
					lottery.setLotteryDate(new Timestamp(timestamp));

					String lotteryNumbers = o.getString("lottery_numbers");
					if (dealLotteryNumbers(lotteryType)) {
						lotteryNumbers = lotteryNumbers.substring(0, lotteryNumbers.lastIndexOf(","));
					}
					lottery.setLotteryNumbers(lotteryNumbers);

					if (dealContent(lotteryType)) {
						JSONArray array = o.getJSONArray("content");
						lottery.setLotteryContent(array.toJSONString());
					} else {
						lottery.setLotteryContent(o.getString("content"));
					}
					lotteryList.add(lottery);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lotteryList;
	}
}
